<?php

/**
 * definition actions.
 *
 * @package    explications_informatiques
 * @subpackage definition
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class definitionActions extends sfActions
{
	

  public function executeNew(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_DEF"),"headers","forbidden");
    $this->form = new lexiqueDefinitionForm();
	$this->form->setDefault('id_mot', $request->getParameter('id'));
  }

  public function executeCreate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_DEF"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST),"headers","forbidden");

    $this->form = new lexiqueDefinitionForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MODERATION_DEFS"),"headers","forbidden");
    $this->forward404Unless($lexique_definition = Doctrine_Core::getTable('lexiqueDefinition')->find(array($request->getParameter('idlexique_definition'))), sprintf('Object lexique_definition does not exist (%s).', $request->getParameter('idlexique_definition')));
    $this->form = new lexiqueDefinitionForm($lexique_definition);
  }

  public function executeUpdate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MODERATION_DEFS"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT),"headers","forbidden");
    $this->forward404Unless($lexique_definition = Doctrine_Core::getTable('lexiqueDefinition')->find(array($request->getParameter('idlexique_definition'))), sprintf('Object lexique_definition does not exist (%s).', $request->getParameter('idlexique_definition')));
    $this->form = new lexiqueDefinitionForm($lexique_definition);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MODERATION_DEFS"),"headers","forbidden");

    $this->forward404Unless($lexique_definition = Doctrine_Core::getTable('lexiqueDefinition')->find(array($request->getParameter('idlexique_definition'))), sprintf('Object lexique_definition does not exist (%s).', $request->getParameter('idlexique_definition')));
    $lexique_definition->delete();
	//log
		sfCoreAutoload::logElem("Suppression d'une def id:".$request->getParameter('idlexique_definition'), $this->getUser()->getGuardUser()->getId(), date("Y-m-d h:i:s"));
	$this->getUser()->setFlash("info","Définition supprimée");
    $this->redirect('lexique/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $ld = new Lexiquedefinition();
	  $ld->setIdMot($request["ld"]["id_mot"]);
	  $ld->setIdAuteur($this->getUser()->getGuardUser()->getId());
	  $ld->setDefinition($request["ld"]["definition"]);
	  $ld->replace();
	  
	  if($request->getParameter('action') == "create")
	  	$this->getUser()->setFlash("info","Définition créée");
	  else
	  	$this->getUser()->setFlash("info","Définition modifiée");
	  $this->getUser()->updatePoint(40, "Ajout d'une définition");	
      $this->redirect('search/lexique?id='.$request["ld"]["id_mot"]);
    }
  }
}
